var app = getApp();
var find_part_ids;
var find_person_ids;
Page({

  /**
   * 页面的初始数据
   */
  data: {    
    cur_part_id:-1,   //选择移入的部门id
    cut_part_name:''  //选择移入的部门名称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type,    //1设置主管，2移动成员，3选择部门
      lay_num:parseInt(options.lay_num),   //当前层级
      page_num:parseInt(options.page_num)   //进入此页时的层级
    });
    wx.setNavigationBarTitle({
      title:options.check_name
    });
    if(options.type==1){
      this.setData({
        id:options.id,   //当前部门id
        load_num:1     
      });
      this.loadlist();
    }else if(options.type==2){    
      console.log(options);
      this.setData({
        id:options.id,   //当前部门id
        load_num:1
      });
      this.loadlist();
    }else if(options.type==3){
      console.log(options);
      var notchoose=options.part_str.split(',');
      let not_objchose = {};
      notchoose.map((e) => {
        not_objchose[e] =true;
      });
      this.setData({
        not_objchose:not_objchose,
        person_str:options.person_str,
        part_str:options.part_str
      })
      this.get_part();
    };  
  },
  loadlist:function(){
    var that = this;
    that.setData({
      load_num:0
    });
    if(that.data.lay_num==0){ //根目录
      app.AppHttp('api/Client/structureManage', {
        id:app.globalData.loginInfo.id
      }, function (res) {
        console.log(JSON.stringify(res.data));
        that.setData({
          person_list: res.data.list,
          children_part:res.data.department
        }); 
      });
    }else if(that.data.lay_num>0){   //当前层级
      app.AppHttp('api/Client/searchChildrenPart', {
        id:that.data.id
      }, function (res) {
        console.log(JSON.stringify(res.data));
        that.setData({
          children_part:res.data.part_list,
          person_list:res.data.person_list    
        });   
      });
    };     
  },
  // 查看用户
   itemClick: function (e) {
    return;
    var index = e.currentTarget.id;
    var yg = this.data.person_list[index];
    if (yg.is_validate == 1){
      wx.navigateTo({
        url: '../yuangongshezhi/yuangongshezhi?id='+yg.id+'&nick_name='+yg.nickname
      });    
    }
  },
  // 接受员工
  jieshouClick:function(event){
    var index = event.currentTarget.id;
    var yg = this.data.person_list[index];
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定接受？',
      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.url + 'api/Client/examineEmployee',
            method: "POST",
            data: {
              openid: app.globalData.openid,
              id: yg.id
            },
            success: function (res) {
              console.log(JSON.stringify(res.data));
              yg.is_validate = 1;
              that.setData({
                ["person_list[" + index + "]"]: yg
              });
            }
          });
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    });   
  },
  // 去下一级部门
  todepart:function(e){
    if(e.currentTarget.dataset.son==0){
      return;
    };
    var index=e.currentTarget.dataset.index;
    var id=this.data.children_part[index].id;
    var lay_num=parseInt(this.data.lay_num)+1; 
    wx.navigateTo({
      url:"../check_depart/check_depart?type="+2+'&check_name='+'移动成员'+'&lay_num='+lay_num+'&id='+id+'&page_num='+this.data.page_num
    });
  },
  // 移动成员(选员工)
  ChangeOne_person:function(e){
    console.log("click");
    var cur_id=e.currentTarget.dataset.id;   //当前点击选项id
    var cur_img=e.currentTarget.dataset.img;  //当前点击项头像
    var person_obj=this.data.person_obj;
    var all_index=this.data.all_index;
    if(person_obj[cur_id]){
      delete person_obj[cur_id];
      all_index = all_index.filter(function (item) {
        return (item.type==1 || item.id!=cur_id);
      });
    }else{
      person_obj[cur_id]=true;
      all_index.push({type:0,img:cur_img,id:cur_id});  
    };
    var person_ids=Object.keys(person_obj);
    console.log(person_obj);
    console.log(person_ids);
    console.log(all_index);
    wx.setStorageSync('person_ids',person_ids) //存 
    wx.setStorageSync('person_obj',person_obj) //存 
    wx.setStorageSync('all_index',all_index) //存 
    this.setData({
      person_ids:person_ids,
      person_obj:person_obj,
      all_index:all_index
    });
  },
  // 移动成员(选部门)
  ChangeOne_part:function(e){
    var cur_id=e.currentTarget.dataset.id;   //当前点击选项id
    var cur_name=e.currentTarget.dataset.cur_name; //当前点击部门名称
    var children_obj=this.data.children_obj;
    var all_index=this.data.all_index;
    if(children_obj[cur_id]){
      delete children_obj[cur_id];
      all_index = all_index.filter(function (item) {
        return (item.type==0 || item.id!=cur_id);
      });
      var children_part_ids=Object.keys(children_obj);
      wx.setStorageSync('children_part_ids',children_part_ids) //存 
      wx.setStorageSync('children_obj',children_obj) //存 
      wx.setStorageSync('all_index',all_index) //存 
      this.setData({
        children_part_ids:children_part_ids,
        children_obj:children_obj,
        all_index:all_index
      });
    }else{
      children_obj[cur_id]=true;
      all_index.push({type:1,name:cur_name,id:cur_id});
      this.find_par_per(cur_id,children_obj,all_index);
      return;
    };
  },
  find_par_per:function(cur_id,children_obj,all_index){
    var that = this;
    var person_obj=that.data.person_obj;
    app.AppHttp('api/Client/serachChilrenPartAndPersonalList', {
      id:cur_id
    }, function (res) {
      console.log(JSON.stringify(res.data)); 
      if(res.data.part_str!=''){
        find_part_ids=res.data.part_str.split(',');
        console.log(find_part_ids);
        if(find_part_ids.length>0){
          for(var i in find_part_ids){
            console.log(find_part_ids[i]);
            delete children_obj[find_part_ids[i]];
            all_index = all_index.filter(function (item) {
              if(item.type==1 && (item.id==cur_id || item.id!=find_part_ids[i]) || item.type==0){
                console.log(item);
              };
              return (item.type==1 && (item.id==cur_id || item.id!=find_part_ids[i]) || item.type==0);
            });
          };
        };
      };
      if(res.data.person_str!=''){
        find_person_ids=res.data.person_str.split(','); 
        console.log(find_person_ids);
        if(find_person_ids.length>0){
          for(var j in find_person_ids){
            console.log(find_person_ids[j]);
            delete person_obj[find_person_ids[j]];
            all_index = all_index.filter(function (item) {
              if(item.type==1 || item.type==0 && item.id!=find_person_ids[j]){
                console.log(item);
              }
              return (item.type==1 || item.type==0 && item.id!=find_person_ids[j]);
            });
          };
        };
      };  
      var person_ids=Object.keys(person_obj);
      var children_part_ids=Object.keys(children_obj);
      console.log(person_obj);
      console.log(person_ids);
      console.log(children_obj);
      console.log(children_part_ids);
      console.log(all_index);
      wx.setStorageSync('person_ids',person_ids) //存 
      wx.setStorageSync('person_obj',person_obj) //存 
      wx.setStorageSync('children_part_ids',children_part_ids) //存 
      wx.setStorageSync('children_obj',children_obj) //存 
      wx.setStorageSync('all_index',all_index) //存 
      that.setData({
        person_ids:person_ids,
        children_part_ids:children_part_ids,
        person_obj:person_obj,
        children_obj:children_obj,
        all_index:all_index
      });
    });
  },
  // 取消所选择的移动成员
  del_chose_item:function(e){
    var index=e.currentTarget.dataset.index;
    var type=e.currentTarget.dataset.type;
    var all_index=this.data.all_index;
    var cur_id=all_index[index].id;
    var person_ids=this.data.person_ids;
    var children_part_ids=this.data.children_part_ids;
    var children_obj=this.data.children_obj;
    var person_obj=this.data.person_obj;
    if(type==0){  //员工
      person_obj[cur_id]=false;
      person_ids=(Object.keys(person_obj)).filter((key)=>{  
        return person_obj[key]==true;
      });
    }else if(type==1){  //部门
      children_obj[cur_id]=false;
      children_part_ids=(Object.keys(children_obj)).filter((key)=>{  
        return children_obj[key]==true;
      });
    };
    all_index.splice(index,1);
    wx.setStorageSync('children_part_ids',children_part_ids) //存 
    wx.setStorageSync('person_ids',person_ids) //存 
    wx.setStorageSync('children_obj',children_obj) //存 
    wx.setStorageSync('person_obj',person_obj) //存 
    wx.setStorageSync('all_index',all_index) //存 
    console.log(person_obj);
    console.log(person_ids);
    console.log(children_obj);
    console.log(children_part_ids);
    console.log(all_index);
    this.setData({
      person_obj:person_obj,
      children_obj:children_obj,
      person_ids:person_ids,
      children_part_ids:children_part_ids,
      all_index:all_index
    });
  },
  // 选择设为主管
  ChangePerson:function(e){
    this.setData({
      charge_id:e.detail.value
    });
  },
  // 设为主管
  set_zhuguan_but:function(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定设为主管？',
      success: function (sm) {
        if (sm.confirm) {
          app.AppHttp('api/Client/setCharge', {
            charge_id:that.data.charge_id,
            id:that.data.id
          }, function (res) {
            console.log(JSON.stringify(res.data));
            wx.navigateBack({
              delta:1
            });   
          });
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },
  // 去移动
  tomove_but:function(){
    var children_part_ids=this.data.children_part_ids;
    var person_ids=this.data.person_ids;
    if(children_part_ids.length>0){
      children_part_ids = children_part_ids.filter(function (item) {  //数组过滤
        //元素值，元素的索引，原数组。    
        return item!='';       
      });
    };
    if(person_ids.length>0){
      person_ids = person_ids.filter(function (item) {  //数组过滤
        //元素值，元素的索引，原数组。    
        return item!='';       
      });
    };
    if(person_ids.length==0 && children_part_ids.length==0){
      wx.showLoading({
        title:'至少选择一项',
        duration:500
      });
      return;
    };
    console.log(person_ids);
    console.log(children_part_ids);
    var lay_num=parseInt(this.data.lay_num)+1;  
    var page_num=this.data.page_num  //进入此页时的层级
    console.log(lay_num);
    console.log(page_num);
    wx.navigateTo({
      url:"../check_depart/check_depart?type="+3+'&check_name='+'选择部门'+'&person_str='+person_ids+'&part_str='+children_part_ids+'&lay_num='+lay_num+'&page_num='+page_num
    });
  },
  // 获取部门
  get_part:function(){
    var that = this;
    app.AppHttp('api/Client/getPart', {
      client_id:app.globalData.loginInfo.id
    }, function (res) {
      console.log(JSON.stringify(res.data));
      that.setData({
        children_part:res.data
      }); 
    }); 
  },
  // 选择要移入的部门
  ChangePart:function(e){
    console.log(e.detail.value);
    var cur_item=(e.detail.value).split(',');
    this.setData({
      cur_part_id:cur_item[0],
      cut_part_name:cur_item[1]
    });
  },
  // 取消选择要移入的部门
  del_chose_move:function(){
    this.setData({
      cur_part_id:-1,
      cut_part_name:''
    });
  },
  // 确定移动
  conf_move_but:function(){
    var that = this;
    var person_str=that.data.person_str;
    var part_str=that.data.part_str;
    console.log(that.data.cur_part_id);
    if(that.data.cur_part_id==-1){
      wx.showLoading({
        title:'至少选择一项',
        duration:500
      });
      return;
    };
    console.log(that.data.cur_part_id);
    console.log(person_str);
    console.log(part_str);
    app.AppHttp('api/Client/batchMovePerson', {
      id:that.data.cur_part_id,
      person_str:person_str,
      part_str:part_str
    }, function (res) {
      console.log(JSON.stringify(res.data));
      wx.showToast({
        title:res.data.status
      });
      console.log(that.data.lay_num);
      console.log(that.data.page_num);
      var back_lay=parseInt(that.data.lay_num)-parseInt(that.data.page_num)+1;  //返回层级
      console.log(back_lay);
      wx.navigateBack({
        delta:back_lay
      })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.type<3 && this.data.load_num==0){
      if(this.data.type==2){
        this.setData({
          person_obj:wx.getStorageSync('person_obj'),
          children_obj:wx.getStorageSync('children_obj'),
          person_ids:wx.getStorageSync('person_ids'),
          children_part_ids:wx.getStorageSync('children_part_ids'),
          all_index:wx.getStorageSync('all_index')
        });
      }
      this.loadlist();
    };
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})